package com.hoshiicloud.user.service.impl.store;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.user.entity.store.PlatStore;
import com.hoshiicloud.user.entity.store.StoreGoodsCategory;
import com.hoshiicloud.user.feign.GoodsService;
import com.hoshiicloud.user.feign.IdGeneratorService;
import com.hoshiicloud.user.mapper.store.StoreGoodsCategoryMapper;
import com.hoshiicloud.user.service.store.PlatStoreService;
import com.hoshiicloud.user.service.store.StoreGoodsCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 店铺经营类目 服务实现类
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-04-28
 */
@Slf4j
@Service
public class StoreGoodsCategoryServiceImpl extends ServiceImpl<StoreGoodsCategoryMapper, StoreGoodsCategory> implements StoreGoodsCategoryService {
    @Autowired
    private PlatStoreService platStoreService;
    @Autowired
    private IdGeneratorService idGeneratorService;
    @Autowired
    private GoodsService goodsService;

    /**
     * @return IPage
     * @description: 分页查询店铺经营类目
     */
    @Logable(authName = "店铺管理-店铺经营类目管理-分页查询店铺经营类目")
    @Cacheable(cacheNames = "StoreGoodsCategory",key ="#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    public IPage<StoreGoodsCategory> getPage(IPage page, QueryWrapper queryWrapper,String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * @param id
     * @return com.hoshiicloud.store.entity.StoreGoodsCategory
     * @description: 根据id查询店铺经营类目
     */
    @Cacheable(cacheNames = "StoreGoodsCategory", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "店铺管理-店铺经营类目 -查询店铺经营类目 ")
    public StoreGoodsCategory getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * @return boolean
     * @description: 新增店铺经营类目
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "StoreGoodsCategory", allEntries = true)
    @Logable(authName = "店铺管理-店铺经营类目-新增店铺经营类目")
    public boolean save(StoreGoodsCategory entity) {
        Response response = goodsService.getGoodsCategoryById(entity.getCategoryId());
        if (response == null || response.getCode() != 200 || response.getData() == null) {
            throw new ServiceException(CommonError.error("新增失败，没有找到对应商品类目"));
        }

        PlatStore platStore = platStoreService.getById(entity.getStoreId());
        if (platStore == null) {
            throw new ServiceException(CommonError.error("新增失败，没有找到对应店铺"));
        }

        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
     * @return boolean
     * @description: 修改店铺经营类目
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "StoreGoodsCategory", allEntries = true)
    @Logable(authName = "店铺管理-店铺经营类目管理-修改店铺经营类目")
    public boolean updateById(StoreGoodsCategory entity) {
        Response response = goodsService.getGoodsCategoryById(entity.getCategoryId());
        if (response == null || response.getCode() != 200 || response.getData() == null) {
            throw new ServiceException(CommonError.error("修改失败，没有找到对应商品类目"));
        }

        PlatStore platStore = platStoreService.getById(entity.getStoreId());
        if (platStore == null) {
            throw new ServiceException(CommonError.error("修改失败，没有找到对应店铺"));
        }

        entity.setUpdatedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }



    @Cacheable(cacheNames = "StoreGoodsCategory", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "店铺管理-店铺经营类目-根据id集合查询店铺经营类目")
    public List<StoreGoodsCategory> getByIds(List<Long> ids, String key) {
        return baseMapper.selectBatchIds(ids);
    }
}
